package com.zx.base.ctrl;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.zx.tradeclient.R;


public abstract class BaseGroup extends ActivityGroup {
	
	private LinearLayout container = null;

	/** 源Intent */
	protected Intent fromIntent;

	/** 功能模块跳转的目标Intent */
	protected Intent targetIntent = new Intent();

	/** 所有导航按钮的ID */
	private int[] navIds = null;

	/** 标签ID对应的初始Activity集合 */
	private Map<Integer, Class<? extends Activity>> classes = new HashMap<Integer, Class<? extends Activity>>();

	/** 标签ID对应的当前Activity集合 */
	public Map<Integer, Class<? extends Activity>> currentClasses = new HashMap<Integer, Class<? extends Activity>>();

	/** 在子类中需要实现的获取选项卡所有标签ID对应的初始Activity的方法 */
	public abstract Class<? extends Activity>[] getClasses();

	/** 在子类中需要实现的获取选项卡所有标签的ID的方法 */
	protected abstract int[] getNavIds();

	/**
	 * 在子类中实现的设定布局的方法，Activity的布局的id必须为R.id.tab_content
	 */
	protected abstract int getLayoutResourceId();
	
	protected abstract int getCheckedButtonId();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getLayoutResourceId());

		// 获取源Intent
		fromIntent = getIntent();
		navIds = getNavIds();
		for (int i = 0; i < navIds.length; i++) {
			classes.put(navIds[i], getClasses()[i]);
			currentClasses.put(navIds[i], getClasses()[i]);
		}
		container = (LinearLayout) findViewById(R.id.tab_content);
	}

	/** 设定目标Intent的方法 */
	protected void setTargetIntent(int checkedId) {
		targetIntent.setClass(BaseGroup.this, currentClasses.get(checkedId));
	}

	/** ActivityGroup加载新的子Activity的方法(创建新的) */
	public void launchNewActivity(Intent intent) {
		container.removeAllViews();
		container.addView(getLocalActivityManager().startActivity(
						intent.getComponent().getShortClassName()+ getCheckedButtonId(),
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
	}

	/** ActivityGroup加载新的子Activity的方法(创建新的) */
	public void launchNewActivityForResult(BaseActivity requestSubActivity,
			Intent intent, int requestCode) {
		intent.putExtra("requestCode", requestCode);
		container.removeAllViews();
		container
				.addView(getLocalActivityManager().startActivity(
						intent.getComponent().getShortClassName()+ getCheckedButtonId(),
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
		((BaseActivity) getCurrentActivity()).setRequestSubActivity(requestSubActivity);
	}

	/** ActivityGroup加载子Activity的方法(先看有没有，有则加载原来的，否则创建新的) */
	public void launchActivity(Intent intent) {
		container.removeAllViews();
		container.addView(getLocalActivityManager().startActivity(
				intent.getComponent().getShortClassName()+ getCheckedButtonId(),
				intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))
				.getDecorView());
	}
}
